package team.stride.tabs.records;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import team.stride.R;
import team.stride.database.StrideDB;
import team.stride.database.crud.playlists.PlaylistsItem;
import team.stride.database.crud.results.ResultsItem;
import team.stride.database.crud.tracks.TracksItem;
import team.stride.database.crud.users.UsersItem;
import team.stride.utils.UtilsStride;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class RecordsActivity extends Activity
{
	private static final int PLAYLIST = 0;
	private static final int DISTANCE = 1;
	private static final int SPEED = 2;
	private static final int EVOLUTION = 3;
	private static final int TRACK = 4;
	private static final String startHTMLTags = "<html><body><font color='#eeeeee'>";
	private static final String endHTMLTags = "</font></body></html>";
	private WebView webView;
	private Spinner choose;

	Comparator<ReportTrack> comparatorReportTrack = new Comparator<ReportTrack>()
	{
		@Override
		public int compare(ReportTrack o1, ReportTrack o2)
		{
			return o2.getResults().size() - o1.getResults().size();
		}
	};
	
	Comparator<ReportPlaylist> comparatorReportPlaylist = new Comparator<ReportPlaylist>()
	{
		@Override
		public int compare(ReportPlaylist o1, ReportPlaylist o2)
		{
			return o2.getUsers().size() - o1.getUsers().size();
		}
	};
	
	Comparator<ResultsItem> comparatorResultsItem = new Comparator<ResultsItem>()
	{

		@Override
		public int compare(ResultsItem o1, ResultsItem o2)
		{
			return (int) (o2.getLength() - o1.getLength());
		}
	};
	Comparator<ResultsItem> comparatorResultsItemDate = new Comparator<ResultsItem>()
	{

		@Override
		public int compare(ResultsItem o1, ResultsItem o2)
		{
			return (int) (o2.getDate() - o1.getDate());
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		View v = LayoutInflater.from(getParent()).inflate(R.layout.bd_reports, null);
		setContentView(v);

		webView = (WebView) findViewById(R.id.reportPlaylist);
		webView.setBackgroundColor(0);
		choose = (Spinner) findViewById(R.id.reportChoose);

		String[] array_spinner = { "Playlist", "Distance", "Speed", "Evolution", "Track" };
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
				array_spinner);
		choose.setAdapter(adapter);
		choose.setOnItemSelectedListener(new OnItemSelectedListener()
		{
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long arg3)
			{
				initView(pos);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0)
			{
			}
		});
		choose.setSelection(0);
		// initView(PLAYLIST);
	}

	@Override
	public void onResume()
	{
		super.onResume();
		if (UtilsStride.resultsChanged)
		{
			initView(choose.getSelectedItemPosition());
		}
	}

	private void initView(int index)
	{
		switch (index)
		{
		case PLAYLIST:
		{
		
			ArrayList<ReportPlaylist> recordsList = new ArrayList<ReportPlaylist>();
			ArrayList<PlaylistsItem> playlists = new ArrayList<PlaylistsItem>();

			// pentru fiecare playlist pl1
			Cursor cursor = managedQuery(StrideDB.CONTENT_URI_PLAYLISTS, null, null, null, null);
			while (cursor.moveToNext())
			{
				final int name_index = cursor.getColumnIndex(StrideDB.NAME_P);
				final int details_index = cursor.getColumnIndex(StrideDB.DETAILS_P);

				String name = cursor.getString(name_index);
				String details = cursor.getString(details_index);
				playlists.add(new PlaylistsItem(name, details));
			}
			cursor.close();

			for (PlaylistsItem playlistsItem : playlists)
			{
				ArrayList<UsersItem> uList = new ArrayList<UsersItem>();
				cursor = managedQuery(StrideDB.CONTENT_URI_USERS, null, StrideDB.PLAYLIST_U + " = '"
						+ playlistsItem.getName() + "'", null, null);
				while (cursor.moveToNext())
				{
					final int id_index = cursor.getColumnIndex(StrideDB.ID_U);
					final int name_index = cursor.getColumnIndex(StrideDB.NAME_U);
					final int network_index = cursor.getColumnIndex(StrideDB.NETWORK_U);
					final int playlist_index = cursor.getColumnIndex(StrideDB.PLAYLIST_U);
					final int details_index = cursor.getColumnIndex(StrideDB.DETAILS_U);

					int id = cursor.getInt(id_index);
					String name = cursor.getString(name_index);
					String playlist = cursor.getString(playlist_index);
					String network = cursor.getString(network_index);
					String details = cursor.getString(details_index);
					uList.add(new UsersItem(id, name, network, playlist, details));
				}
				cursor.close();
				recordsList.add(new ReportPlaylist(playlistsItem, uList));
			}

			Collections.sort(recordsList, comparatorReportPlaylist);

			// print report
			StringBuilder text = new StringBuilder(startHTMLTags);
			setReportTitle(text, "Most Used Playlists and Users");
			for (ReportPlaylist raportPlaylist : recordsList)
			{
				text.append(raportPlaylist.toHTMLString() + "<br/>");
			}
			text.append(endHTMLTags);
			webView.loadData(text.toString(), "text/html", "utf-8");
			webView.reload();
			break;
		}
		case DISTANCE:
		{
			ArrayList<ReportDistance> recordsList = new ArrayList<ReportDistance>();
			ArrayList<UsersItem> uList = new ArrayList<UsersItem>();

			// pentru fiecare user u1
			Cursor cursor = managedQuery(StrideDB.CONTENT_URI_USERS, null, null, null, null);
			while (cursor.moveToNext())
			{
				final int id_index = cursor.getColumnIndex(StrideDB.ID_U);
				final int name_index = cursor.getColumnIndex(StrideDB.NAME_U);
				final int network_index = cursor.getColumnIndex(StrideDB.NETWORK_U);
				final int playlist_index = cursor.getColumnIndex(StrideDB.PLAYLIST_U);
				final int details_index = cursor.getColumnIndex(StrideDB.DETAILS_U);

				int id = cursor.getInt(id_index);
				String name = cursor.getString(name_index);
				String playlist = cursor.getString(playlist_index);
				String network = cursor.getString(network_index);
				String details = cursor.getString(details_index);
				uList.add(new UsersItem(id, name, network, playlist, details));
			}
			cursor.close();

			for (UsersItem usersItem : uList)
			{
				
				ArrayList<ResultsItem> rList = new ArrayList<ResultsItem>();
				cursor = managedQuery(StrideDB.CONTENT_URI_RESULTS, null, StrideDB.USERNAME_R + " = '"
						+ usersItem.getName() + "'", null, null);
				while (cursor.moveToNext())
				{
					final int id_index = cursor.getColumnIndex(StrideDB.ID_R);
					final int name_index = cursor.getColumnIndex(StrideDB.NAME_R);
					final int username_index = cursor.getColumnIndex(StrideDB.USERNAME_R);
					final int date_index = cursor.getColumnIndex(StrideDB.DATE_R);
					final int laps_index = cursor.getColumnIndex(StrideDB.LAPS_R);
					final int length_index = cursor.getColumnIndex(StrideDB.LENGTH_R);
					final int time_index = cursor.getColumnIndex(StrideDB.TIME_R);
					final int topSpeed_index = cursor.getColumnIndex(StrideDB.TOPSPEED_R);
					final int details_index = cursor.getColumnIndex(StrideDB.DETAILS_R);

					int id = cursor.getInt(id_index);
					String name = cursor.getString(name_index);
					String username = cursor.getString(username_index);
					long date = cursor.getLong(date_index);
					float laps = cursor.getFloat(laps_index);
					float length = cursor.getFloat(length_index);
					int time = cursor.getInt(time_index);
					float topSpeed = cursor.getFloat(topSpeed_index);
					String details = cursor.getString(details_index);

					rList.add(new ResultsItem(id, name, username, date, laps, length, time, topSpeed, details));
				}
				cursor.close();
				Collections.sort(rList, comparatorResultsItem);
				recordsList.add(new ReportDistance(usersItem, rList));
			}

			// print report
			StringBuilder text = new StringBuilder(startHTMLTags);
			setReportTitle(text, "Sorted Distance per User");
			for (ReportDistance raportDistance : recordsList)
			{
				text.append(raportDistance.toHTMLString() + "<br/>");
			}
			text.append(endHTMLTags);
			webView.loadData(text.toString(), "text/html", "utf-8");
			webView.reload();
			break;
		}
		case SPEED:
		{
			ArrayList<ReportSpeed> recordsList = new ArrayList<ReportSpeed>();
			ArrayList<ResultsItem> rList = new ArrayList<ResultsItem>();

			Cursor cursor = managedQuery(StrideDB.CONTENT_URI_RESULTS, null, null, null, null);
			while (cursor.moveToNext())
			{
				final int id_index = cursor.getColumnIndex(StrideDB.ID_R);
				final int name_index = cursor.getColumnIndex(StrideDB.NAME_R);
				final int username_index = cursor.getColumnIndex(StrideDB.USERNAME_R);
				final int date_index = cursor.getColumnIndex(StrideDB.DATE_R);
				final int laps_index = cursor.getColumnIndex(StrideDB.LAPS_R);
				final int length_index = cursor.getColumnIndex(StrideDB.LENGTH_R);
				final int time_index = cursor.getColumnIndex(StrideDB.TIME_R);
				final int topSpeed_index = cursor.getColumnIndex(StrideDB.TOPSPEED_R);
				final int details_index = cursor.getColumnIndex(StrideDB.DETAILS_R);

				int id = cursor.getInt(id_index);
				String name = cursor.getString(name_index);
				String username = cursor.getString(username_index);
				long date = cursor.getLong(date_index);
				float laps = cursor.getFloat(laps_index);
				float length = cursor.getFloat(length_index);
				int time = cursor.getInt(time_index);
				float topSpeed = cursor.getFloat(topSpeed_index);
				String details = cursor.getString(details_index);

				rList.add(new ResultsItem(id, name, username, date, laps, length, time, topSpeed, details));
			}
			Collections.sort(rList, comparatorResultsItem);
			recordsList.add(new ReportSpeed(rList));
			// print report
			StringBuilder text = new StringBuilder(startHTMLTags);
			setReportTitle(text, "Results ordered by speed");
			for (ReportSpeed raportSpeed : recordsList)
			{
				text.append(raportSpeed.toHTMLString() + "<br/>");
			}
			text.append(endHTMLTags);
			webView.loadData(text.toString(), "text/html", "utf-8");
			webView.reload();
			break;
		}
		case EVOLUTION:
		{
			ArrayList<ReportEvolution> recordsList = new ArrayList<ReportEvolution>();
			ArrayList<UsersItem> uList = new ArrayList<UsersItem>();

			// pentru fiecare user u1
			Cursor cursor = managedQuery(StrideDB.CONTENT_URI_USERS, null, null, null, null);
			while (cursor.moveToNext())
			{
				final int id_index = cursor.getColumnIndex(StrideDB.ID_U);
				final int name_index = cursor.getColumnIndex(StrideDB.NAME_U);
				final int network_index = cursor.getColumnIndex(StrideDB.NETWORK_U);
				final int playlist_index = cursor.getColumnIndex(StrideDB.PLAYLIST_U);
				final int details_index = cursor.getColumnIndex(StrideDB.DETAILS_U);

				int id = cursor.getInt(id_index);
				String name = cursor.getString(name_index);
				String playlist = cursor.getString(playlist_index);
				String network = cursor.getString(network_index);
				String details = cursor.getString(details_index);
				uList.add(new UsersItem(id, name, network, playlist, details));
			}
			cursor.close();

			for (UsersItem usersItem : uList)
			{
				ArrayList<ResultsItem> rList = new ArrayList<ResultsItem>();
				cursor = managedQuery(StrideDB.CONTENT_URI_RESULTS, null, StrideDB.USERNAME_R + " = '"
						+ usersItem.getName() + "'", null, "'" + StrideDB.DATE_R + "'");
				while (cursor.moveToNext())
				{
					final int id_index = cursor.getColumnIndex(StrideDB.ID_R);
					final int name_index = cursor.getColumnIndex(StrideDB.NAME_R);
					final int username_index = cursor.getColumnIndex(StrideDB.USERNAME_R);
					final int date_index = cursor.getColumnIndex(StrideDB.DATE_R);
					final int laps_index = cursor.getColumnIndex(StrideDB.LAPS_R);
					final int length_index = cursor.getColumnIndex(StrideDB.LENGTH_R);
					final int time_index = cursor.getColumnIndex(StrideDB.TIME_R);
					final int topSpeed_index = cursor.getColumnIndex(StrideDB.TOPSPEED_R);
					final int details_index = cursor.getColumnIndex(StrideDB.DETAILS_R);

					int id = cursor.getInt(id_index);
					String name = cursor.getString(name_index);
					String username = cursor.getString(username_index);
					long date = cursor.getLong(date_index);
					float laps = cursor.getFloat(laps_index);
					float length = cursor.getFloat(length_index);
					int time = cursor.getInt(time_index);
					float topSpeed = cursor.getFloat(topSpeed_index);
					String details = cursor.getString(details_index);

					rList.add(new ResultsItem(id, name, username, date, laps, length, time, topSpeed, details));
				}
				cursor.close();
				Collections.sort(rList, comparatorResultsItemDate);
				recordsList.add(new ReportEvolution(usersItem, rList));
			}
			// print report
			StringBuilder text = new StringBuilder(startHTMLTags);
			setReportTitle(text, "User Evolution (Results by Date)");
			for (ReportEvolution raportEvolution : recordsList)
			{
				text.append(raportEvolution.toHTMLString() + "<br/>");
			}
			text.append(endHTMLTags);
			webView.loadData(text.toString(), "text/html", "utf-8");
			webView.reload();
			break;
		}
		case TRACK:
		{
			

			ArrayList<ReportTrack> recordsList = new ArrayList<ReportTrack>();
			ArrayList<TracksItem> tList = new ArrayList<TracksItem>();

			// pentru fiecare track t1
			Cursor cursor = managedQuery(StrideDB.CONTENT_URI_TRACKS, null, null, null, null);
			while (cursor.moveToNext())
			{
				final int id_index = cursor.getColumnIndex(StrideDB.ID_T);
				final int name_index = cursor.getColumnIndex(StrideDB.NAME_T);
				final int lenght_index = cursor.getColumnIndex(StrideDB.LENGTH_T);
				final int gpx_index = cursor.getColumnIndex(StrideDB.GPX_T);
				final int details_index = cursor.getColumnIndex(StrideDB.DETAILS_T);

				int id = cursor.getInt(id_index);
				String name = cursor.getString(name_index);
				int lenght = cursor.getInt(lenght_index);
				String gpx = cursor.getString(gpx_index);
				String details = cursor.getString(details_index);

				tList.add(new TracksItem(id, name, lenght, gpx, details));
			}
			cursor.close();

			for (TracksItem tracksItem : tList)
			{
				ArrayList<ResultsItem> rList = new ArrayList<ResultsItem>();
				cursor = managedQuery(StrideDB.CONTENT_URI_RESULTS, null, StrideDB.TRACKNAME_R + " = '"
						+ tracksItem.getName() + "'", null, null);
				while (cursor.moveToNext())
				{
					final int id_index = cursor.getColumnIndex(StrideDB.ID_R);
					final int name_index = cursor.getColumnIndex(StrideDB.NAME_R);
					final int username_index = cursor.getColumnIndex(StrideDB.USERNAME_R);
					final int date_index = cursor.getColumnIndex(StrideDB.DATE_R);
					final int laps_index = cursor.getColumnIndex(StrideDB.LAPS_R);
					final int length_index = cursor.getColumnIndex(StrideDB.LENGTH_R);
					final int time_index = cursor.getColumnIndex(StrideDB.TIME_R);
					final int topSpeed_index = cursor.getColumnIndex(StrideDB.TOPSPEED_R);
					final int details_index = cursor.getColumnIndex(StrideDB.DETAILS_R);

					int id = cursor.getInt(id_index);
					String name = cursor.getString(name_index);
					String username = cursor.getString(username_index);
					long date = cursor.getLong(date_index);
					float laps = cursor.getFloat(laps_index);
					float length = cursor.getFloat(length_index);
					int time = cursor.getInt(time_index);
					float topSpeed = cursor.getFloat(topSpeed_index);
					String details = cursor.getString(details_index);

					rList.add(new ResultsItem(id, name, username, date, laps, length, time, topSpeed, details));
				}
				cursor.close();
				recordsList.add(new ReportTrack(tracksItem, rList));
			}
			Collections.sort(recordsList, comparatorReportTrack);
			// print report
			StringBuilder text = new StringBuilder(startHTMLTags);
			setReportTitle(text, "Most used Tracks (Results on the tracks)");
			for (ReportTrack reportTrack : recordsList)
			{
				text.append(reportTrack.toHTMLString() + "<br/>");
			}
			text.append(endHTMLTags);
			webView.loadData(text.toString(), "text/html", "utf-8");
			webView.reload();
			break;
		}
		default:
			break;
		}

	}

	private void setReportTitle(StringBuilder text, String title)
	{
		text.append("<font color='#fe6700'>" + title + "</font><br/>");
	}
}
